﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using DataObjectsPhone;
using System.Diagnostics;
using Microsoft.Phone.Net.NetworkInformation;

namespace ee_wp_tourmaker.WebServices
{
    public class DetailedChallengeService
    {
        Guid downloadID;

        public void downloadChallengeWithID(Guid id)
        {
            bool isAvailable = NetworkInterface.GetIsNetworkAvailable();
            if (isAvailable == false)
            {
                Debug.WriteLine("No connection to download more details");
            }
            else
            {
                downloadID = id;
                var client = new ee_wp_tourmaker.AzureService.Service1Client();
                client.OpenCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_OpenCompleted);
                client.OpenAsync(client);
                client.GetChallengeCompleted +=new EventHandler<AzureService.GetChallengeCompletedEventArgs>(client_GetChallengeCompleted);
            
            }
            
        }

        void client_OpenCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            var client = (ee_wp_tourmaker.AzureService.Service1Client)e.UserState;
            client.GetChallengeAsync(downloadID,client);
        }

        void client_GetChallengeCompleted(object sender, AzureService.GetChallengeCompletedEventArgs result)
        {
            String description = result.Result.Description;
            ObservableCollection<CheckPoint> array = new ObservableCollection<CheckPoint>();
            foreach (AzureService.CheckPoint point in result.Result.CheckPoints)
            {
                CheckPoint p = new CheckPoint();
                p.Name = point.Name;
                p.Latitude = point.Latitude;
                p.Longitude = point.Longitude;
                p.Points = point.Points;
                p.WikiLink = point.WikiLink;
                p.Accuracy = point.Accuracy;
                p.Address = point.Address;
                p.ChallengeId = App.MainModel.SelectedChallenge.Id;
                p.Picture = point.Picture;
                p.Done = false;
                p.OtherLink = point.OtherLink;
                p.Description = point.Description;
                array.Add(p);
            }
            App.MainModel.SelectedChallenge.checkpointsList = array;
            App.MainModel.SelectedChallenge.Description = description;
            App.MainModel.SelectedChallenge.MinScore = result.Result.MinScore;
            App.MainModel.ChallengePivot.setChallenge();
            ((AzureService.Service1Client)result.UserState).CloseAsync();

        }
    }
}
